﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.MBSM.DBUtility;
using System.Configuration;
public partial class Manage_GoodsLimitPriceUpdate : System.Web.UI.Page
{

    public string name, medicinemodel, outlookc, strConditions;


    protected void Page_Load(object sender, EventArgs e)
    {

        string strSql;

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)System.Web.HttpContext.Current.Session["Account"];

            if (_Account.Type.Contains("1") || _Account.AccountName.ToUpper().Contains("XJ"))
            {

            }
            else
            {

                Response.Write("<script>alert('非价格人员请勿探望！');window.opener=null;window.open('','_self');window.close();<>");
                Response.Redirect("../HomePage/Default.aspx");

            }
            if (!IsPostBack)
            {

                Db DbProject = new Db();
                strSql = "SELECT AccountId,AccountName FROM ACCOUNT WHERE TYPE=0 OR TYPE=1 OR  TYPE=5 ";
                System.Data.IDataReader dr = DbProject.DataReader(strSql);

                dr.Close();
                dr.Dispose();

                txtname.Text = "";
                txtmedicinemodel.Text = "";
                txtoutlook.Text = "";

                strSql = "select projectid,ProjectMarker,Title from project order by projectid desc";
                dr = DbProject.DataReader(strSql);
                listProject.DataSource = dr;
                listProject.DataTextField = "Title";
                listProject.DataValueField = "projectid";
                listProject.DataBind();
                dr.Close();
                dr.Dispose();
                DbProject.Close();

                lstQualityLevals.Items.Add("全部");
                lstQualityLevals.Items.Add("普通GMP药品");
                lstQualityLevals.Items.Add("专利药品");
                lstQualityLevals.Items.Add("1999年以来获得国家科学技术奖的药品");
                lstQualityLevals.Items.Add("国家一类新药");
                lstQualityLevals.Items.Add("原研制药品");
                lstQualityLevals.Items.Add("单独定价药品");
                lstQualityLevals.Items.Add("优质优价中成药");
                lstQualityLevals.Items.Add("全国医药工业百强企业的药品");
                lstQualityLevals.Items.Add("中药保护品种");
                lstQualityLevals.Items.Add("欧美认证药品");

                chk1.Visible = false;


            }
            if (Request.Form["sub"] != null && Request.Form["sub"].ToString() == "确定修改")
            {

                Submint();

            }

            showGridView();

        }

    }

    /// <summary>
    /// 将数据绑定到GridView1
    /// </summary>
    public void showGridView()
    {

        string _SelectField = "goodsid,convert(numeric(20,3),LimitPrice) as LimitPrice,productid,ProductCode,GoodsName, ProductName,"
                            + " Outlookc,Unit, factor,medicinemodel,"
                            + "PackUnit,UserCode,companyName,"
                            + "case CheckStatus when'0' then '未审核' "
                            + "when null then '未审核' "
                            + "when '1' then '审核通过' "
                            + "when '2' then '不通过' "
                            + "when '3' then '原始状态' "
                            + "when '4' then '暂时通过' end as CheckStatus,"
                            + "AccountName_TJR,AccountName_SHR,NoPass,case QualityLevels "
                            + ConfigurationManager.AppSettings["QualityLevels"].ToString()
                            + @"  end as qualitylevals,shenhetime,convert(numeric(20,3),RetailPrice) as RetailPreic,memo4 ";
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);

        dbPage.Table = "  v_product_goods LEFT OUTER JOIN Account ON v_product_goods.AccountId_SH = Account.AccountId";                                               //要查询的表明
        dbPage.Key = "goodsid";                                                     //表的主键    
        dbPage.Where = strConditions;                                               //查询条件    
        dbPage.SelectField = _SelectField;                                          //要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);             //要显示第几页   
        dbPage.PageSize = 20;                                                       //没有显示记录条数   
        dbPage.OrderBy = "productname,medicinemodel,outlookc,companyName,goodsid";  //排序字段

        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        dr = null;
        db = null;
        this.DbPageFlip1.DbPage = dbPage;                                           //将数据传给控键

    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {

        string strtemp = " checkstatus=1 and";

        //品目号
        string _strProductCode = txtProductCode.Text.Trim();
        if (_strProductCode != "")
        {

            strtemp = strtemp + " Productid = '" + _strProductCode + "' and";

        }



        //有无限价
        string _iflimit = List_limitP.Text.ToString();
        switch (_iflimit)
        {
            case "全部":
                break;
            case "有":
                strtemp += " isnull(LimitPrice,0)>0 and";
                break;
            case "无":
                strtemp += " isnull(LimitPrice,0)=0 and";
                break;
        }

        //质量层次
        string _QualityLevals = lstQualityLevals.SelectedValue;
        switch (_QualityLevals)
        {

            case "全部":
                break;
            case "普通GMP药品":
                strtemp += " qualitylevels=0 and";
                break;
            case "专利药品":
                strtemp += " qualitylevels=1 and";
                break;
            case "1999年以来获得国家科学技术奖的药品":
                strtemp += " qualitylevels=2 and";
                break;
            case "国家一类新药":
                strtemp += " qualitylevels=3 and";
                break;
            case "原研制药品":
                strtemp += " qualitylevels=4 and";
                break;
            case "单独定价药品":
                strtemp += " qualitylevels=5 and";
                break;
            case "优质优价中成药":
                strtemp += " qualitylevels=6 and";
                break;
            case "全国医药工业百强企业的药品":
                strtemp += " qualitylevels=7 and";
                break;
            case "中药保护品种":
                strtemp += " qualitylevels=8 and";
                break;
            case "欧美认证药品":
                strtemp += " qualitylevels=9 and";
                break;
        }

        //流水号
        string _GoodsId = txtLSH.Text.ToString().Trim();
        if (_GoodsId != "")
        {

            strtemp = strtemp + " GoodsId =" + _GoodsId + " and";

        }

        //品名
        string name = txtname.Text.ToString().Trim();
        if (name != "")
        {

            strtemp = strtemp + " (spellingcode  like '%" + name + "%' or ProductName like '%" + name + "%') and";

        }

        //剂型
        string medicinemodel = txtmedicinemodel.Text.ToString().Trim();
        if (medicinemodel != "")
        {

            strtemp = strtemp + " medicinemodel like '%" + medicinemodel + "%' and";

        }

        //规格
        string outlookc = txtoutlook.Text.ToString().Trim();
        if (outlookc != "")
        {

            strtemp = strtemp + " outlookc like '%" + outlookc + "%' and";

        }

        //生产企业名称
        string companyname = txtCompanyName.Text.ToString().Trim();
        if (companyname != "")
        {

            strtemp = strtemp + " companyName like '%" + companyname + "%' and";

        }

        //单位
        string strunit = TextUnit.Text.ToString().Trim();
        if (strunit != "")
        {

            strtemp += " unit like '%" + strunit + "%'  and";

        }

        //转换系数
        String strXS = txtZhuanhuan.Text.ToString().Trim();
        if (strXS != "")
        {

            strtemp += " factor = '" + strXS + "' and";

        }

        if (strtemp != "")
        {

            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);

        }

        return strtemp;

    }

    public string fctConditions2()
    {

        string strtemp = "";

        //品目号
        string _strProductCode = txtProductCode.Text.Trim();

        strtemp += " a.checkstatus=1 and";

        if (_strProductCode != "")
        {

            strtemp = strtemp + " a.Productid = '" + _strProductCode + "' and";

        }

        //有无限价
        string _iflimit = List_limitP.Text.ToString();
        switch (_iflimit)
        {
            case "全部":
                break;
            case "有":
                strtemp += " isnull(LimitPrice,0)>0 and";
                break;
            case "无":
                strtemp += " isnull(LimitPrice,0)=0 and";
                break;
        }

        //质量层次
        string _QualityLevals = lstQualityLevals.SelectedValue;
        switch (_QualityLevals)
        {

            case "全部":
                break;
            case "普通GMP药品":
                strtemp += " qualitylevels=0 and";
                break;
            case "专利药品":
                strtemp += " qualitylevels=1 and";
                break;
            case "1999年以来获得国家科学技术奖的药品":
                strtemp += " qualitylevels=2 and";
                break;
            case "国家一类新药":
                strtemp += " qualitylevels=3 and";
                break;
            case "原研制药品":
                strtemp += " qualitylevels=4 and";
                break;
            case "单独定价药品":
                strtemp += " qualitylevels=5 and";
                break;
            case "优质优价中成药":
                strtemp += " qualitylevels=6 and";
                break;
            case "全国医药工业百强企业的药品":
                strtemp += " qualitylevels=7 and";
                break;
            case "中药保护品种":
                strtemp += " qualitylevels=8 and";
                break;
            case "欧美认证药品":
                strtemp += " qualitylevels=9 and";
                break;
        }

        //流水号
        string _GoodsId = txtLSH.Text.ToString().Trim();
        if (_GoodsId != "")
        {

            strtemp = strtemp + " a.GoodsId =" + _GoodsId + " and";

        }

        //品名
        string name = txtname.Text.ToString().Trim();
        if (name != "")
        {

            strtemp = strtemp + " (a.spellingcode  like '%" + name + "%' or a.ProductName like '%" + name + "%') and";

        }

        //剂型
        string medicinemodel = txtmedicinemodel.Text.ToString().Trim();
        if (medicinemodel != "")
        {

            strtemp = strtemp + " a.medicinemodel like '%" + medicinemodel + "%' and";

        }

        //规格
        string outlookc = txtoutlook.Text.ToString().Trim();
        if (outlookc != "")
        {

            strtemp = strtemp + " a.outlookc like '%" + outlookc + "%' and";

        }

        //生产企业名称
        string companyname = txtCompanyName.Text.ToString().Trim();
        if (companyname != "")
        {

            strtemp = strtemp + " a.companyName like '%" + companyname + "%' and";

        }

        //单位
        string strunit = TextUnit.Text.ToString().Trim();
        if (strunit != "")
        {

            strtemp += " a.unit like '%" + strunit + "%'  and";

        }

        //转换系数
        String strXS = txtZhuanhuan.Text.ToString().Trim();
        if (strXS != "")
        {
            strtemp += " a.factor = '" + strXS + "' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }

    protected void ToEcxul_Click(object sender, EventArgs e)
    {

        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = "Select a.goodsid as 流水号,a.productname as 通用名,a.GoodsName as 商品名,"
                        + "medicinemodel as 剂型,a.outlookc as 规格,"
                        + "a.factor as 转换系数,case a.QualityLevels  "
                        + ConfigurationManager.AppSettings["QualityLevels"].ToString()
                        + " end as 质量层次 ,a.LimitPrice as 限价,a.UserCode as 企业注册号 ,a.companyName as 生产企业,a.AccountName_TJR as 添加人,"
                        + "case a.CheckStatus when'0' then '未审核' "
                        + "when null then '未审核' "
                        + "when '1' then '审核通过' "
                        + "when '2' then '不通过' "
                        + "when '3' then '原始状态' "
                        + "when '4' then '暂时通过'  end as 审核状态, "
                        + "a.AccountName_SHR as 审核人,a.shenhetime as 审核时间 ,ISNULL(b.memo5,'') AS 联系人,ISNULL(b.Telepone,'') AS 电话 ,"
                        + "ISNULL(b.memo10,'') AS 手机 From v_product_goods as a "
                        + "LEFT OUTER JOIN Account ON a.AccountId_SH = Account.AccountId "
                        + "LEFT OUTER JOIN v_Company AS b on b.UserCode=a.usercode  Where "
                        + fctConditions2()
                        + " Order By a.productname,a.medicinemodel,a.outlookc,a.companyName,a.goodsid";
        dr = db.DataReader(_strSql);
        GridView2.DataSource = dr;
        GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        string filename = System.Web.HttpUtility.UrlEncode("商品审核信息[" + "].xls");
        SysFun.WriteToExcel(GridView2, this.Page, filename);

    }



    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }

    #region IPageDescribe 成员

    public string Name
    {
        get { return "审核商品信息"; }
    }

    public string Describe
    {
        get { return "审核商品信息"; }
    }

    public string ParentName
    {
        get { return "商品维护"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckGoods);
            return _AccountType;
        }
    }
    #endregion


    protected void Button5_Click1(object sender, EventArgs e)
    {

        showGridView();

    }

    protected void Submint()
    {

        try
        {

            string Sql = "",_projectid="0";
            _projectid = listProject.SelectedItem.Value;
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)System.Web.HttpContext.Current.Session["Account"];

            for (int i = 0; i <= this.GridView1.Rows.Count - 1; i++)
            {

                System.Web.UI.WebControls.CheckBox chb = (System.Web.UI.WebControls.CheckBox)this.GridView1.Rows[i].FindControl("chk");

               
              System.Web.UI.WebControls.TextBox txt = (System.Web.UI.WebControls.TextBox)this.GridView1.Rows[i].FindControl("xjxg");

                string xjxg = txt.Text.ToString().Trim();

                if (chb.Checked)
                {

                    if (SysFun.IsNumeric(xjxg) && Convert.ToDouble(xjxg) > 0)
                    {

                        
                       // Sql += "update LimitPriceTable set LimitPrice =" + xjxg + " where goodsid = " + this.GridView1.Rows[i].Cells[0].Text.ToString().Trim() + " and projectid='" + _projectid + "';";
                        Sql += "if exists(select goodsid from LimitPriceTable where goodsid='" + GridView1.DataKeys[i].Values[0].ToString() + "' and projectid='" + _projectid + "') update LimitPriceTable set LimitPrice='" + xjxg + "' where goodsid='" + GridView1.DataKeys[i].Values[0].ToString() + "' and projectid='" + _projectid + "' else  insert limitpricetable(goodsid,projectid,limitprice) values('" + GridView1.DataKeys[i].Values[0].ToString() + "','" + _projectid + "','" + xjxg + "') ;";

                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改",
                            this.GridView1.Rows[i].Cells[0].Text.ToString().Trim(),
                            "流水号为" + GridView1.DataKeys[i].Values[0].ToString() + "的商品的限价原为 " + GridView1.DataKeys[i].Values[1].ToString() + " 被改为" + xjxg + "", "../manage/GoodsLimitPriceUpdate.aspx");

                    }


                }
                chb.Checked = false;

            }

            Db db = new Db();
            db.Command(Sql);
            db.Close();

        }
        catch
        {

            showGridView();

        }

    }
}
